<?php

class Admin_commentController extends Admin_baseController {
	public function listAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$keyword = Request::getParams('keyword');
		$status = Request::getParams('status', -1);
		$uid = Request::getParams('uid');
		$typeId = Request::getParams('typeId');
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		}

		if ($keyword) {
			$where[] = 'content like ?';
			$value[] = '%' . $keyword . '%';
			$url['keyword'] = $keyword;
		}
		if ($status != -1) {
			$where[] = 'status=?';
			$value[] = $status - 1;
			$url['status'] = $status;
		}
		$where[] = 'type=?';
		$value[] = 1;
		if ($typeId) {
			$where[] = 'type_id=?';
			$value[] = $typeId;
			$url['typeId'] = $typeId;
		}
		$total = self::model('comment')->count(null, $where, $value);
		$list = self::model('comment')->limit(($page - 1) * $pageSize, $pageSize)->order('comment_id desc')->getAll($where, $value);
		$ids = [];
		foreach ($list as $key => $value) {
			$ids[] = $value['type_id'];
			$uids[] = $value['uid'];
			$list[$key]['dynamic_title'] = '';
		}
		if ($ids) {
			$ids = implode(',', $ids);
			$dynamicList = self::model('dynamic')->query("SELECT dynamic_id,title FROM wf_dynamic WHERE dynamic_id in  ({$ids})")->fetchAll();
			foreach ($list as $key1 => $value1) {
				foreach ($dynamicList as $key2 => $value2) {
					if ($value1['type_id'] == $value2['dynamic_id']) {
						$list[$key1]['dynamic_title'] = $value2['title'];
					}
				}
			}
		}
		if ($uids) {
			$uids = implode(',', $uids);
			$user = self::model('user')->query("SELECT uid,nickname FROM wf_user WHERE uid in  ({$uids})")->fetchAll();
			foreach ($list as $key1 => $value1) {
				foreach ($user as $key2 => $value2) {
					if ($value1['uid'] == $value2['uid']) {
						$list[$key1]['nickname'] = $value2['nickname'];
					}
				}
			}
		}
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'keyword' => $keyword,
			'total' => $total,
			'status' => $status,
			'uid' => $uid,
			'typeId' => $typeId,
			'statusList' => self::model('comment')->getStatusList(),
			'typeList' => self::model('comment')->getTypeList(),
			'list' => $list,
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)
				->url(Router::getBaseUrl() . '/admin_comment/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}
	public function listgoodsAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$keyword = Request::getParams('keyword');
		$status = Request::getParams('status', -1);
		$uid = Request::getParams('uid');
		$typeId = Request::getParams('typeId');
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		}
		if ($keyword) {
			$where[] = 'content like ?';
			$value[] = '%' . $keyword . '%';
			$url['keyword'] = $keyword;
		}
		if ($status != -1) {
			$where[] = 'status=?';
			$value[] = $status - 1;
			$url['status'] = $status;
		}
		$where[] = 'type=?';
		$value[] = 2;
		if ($typeId) {
			$where[] = 'type_id=?';
			$value[] = $typeId;
			$url['typeId'] = $typeId;
		}
		$total = self::model('comment')->count(null, $where, $value);
		$list = self::model('comment')->limit(($page - 1) * $pageSize, $pageSize)->order('comment_id desc')->getAll($where, $value);
		$ids = [];
		foreach ($list as $key => $value) {
			$ids[] = $value['type_id'];
			$list[$key]['goods_title'] = '';
			$uids[] = $value['uid'];
		}
		if ($ids) {
			$ids = implode(',', $ids);
			$goodsList = self::model('goods')->query("SELECT goods_id,name FROM wf_goods WHERE goods_id in ({$ids})")->fetchAll();
			foreach ($list as $key1 => $value1) {
				foreach ($goodsList as $key2 => $value2) {
					if ($value1['type_id'] == $value2['goods_id']) {
						$list[$key1]['goods_title'] = $value2['name'];
					}
				}
			}
		}
		if ($uids) {
			$uids = implode(',', $uids);
			$user = self::model('user')->query("SELECT uid,nickname FROM wf_user WHERE uid in  ({$uids})")->fetchAll();
			foreach ($list as $key1 => $value1) {
				foreach ($user as $key2 => $value2) {
					if ($value1['uid'] == $value2['uid']) {
						$list[$key1]['nickname'] = $value2['nickname'];
					}
				}
			}
		}
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'keyword' => $keyword,
			'total' => $total,
			'status' => $status,
			'uid' => $uid,
			'typeId' => $typeId,
			'statusList' => self::model('comment')->getStatusList(),
			'typeList' => self::model('comment')->getTypeList(),
			'list' => $list,
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)
				->url(Router::getBaseUrl() . '/admin_comment/listgoods?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}
	public function editAction() {
		$commentId = Request::getParams('commentId');
		$data = [];
		$data['content'] = Request::getParams('content');
		$data['image_list'] = Request::getParams('image_list');
		$data['update_time'] = time();
		$data['status'] = Request::getParams('status');

		$commentInfo = [];
		if ($commentId && !$commentInfo = self::model('comment')->getOne('comment_id=?', $commentId)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		if (Request::isGet()) {
			$this->assign('commentInfo', $commentInfo);
			$this->display();
			exit;
		}
//        $result = articleModel::validata($data);
		//        if($result!==true) {
		//            Response::ajax(NORMAL_ERROR, $result);
		//        }
		if ($data['image_list']) {
			$data['image_list'] = implode(';', $data['image_list']);
		} else {
			$data['image_list'] = '';
		}
		if ($commentId) {
//修改
			if (self::model('comment')->update($data, 'comment_id=?', $commentId)) {
				Response::ajax(200, '修改成功');
			}
		} else {
//新增
			$data['add_time'] = time();
			if (self::model('comment')->insert($data)) {
				Response::ajax(200, '添加成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function statusAction() {
		$commentId = intval(Request::getParams('commentId'));
		$status = intval(Request::getParams('status'));
		if (self::model('comment')->update([
			'status' => $status,
		], 'comment_id=?', $commentId)) {
			Response::ajax(200, '修改成功');
		}
		Response::ajax(NORMAL_ERROR, '修改失败,请稍后再试');
	}

	public function deleteAction() {
		$commentId = intval(Request::getParams('commentId'));
		$commentInfo = self::model('comment')->getOne('comment_id=?', $commentId);
		if (!$commentInfo) {
			Response::ajax(NORMAL_ERROR, '评论信息查看失败,无法删除');
		}
		if (self::model('comment')->delete('comment_id=?', $commentId)) {
			if ($commentInfo['type'] == commentModel::DYNAMIC) {
				self::model('dynamic')->execute("update wf_dynamic set comment_total=comment_total-1 where dynamic_id={$commentInfo['type_id']}");
			} elseif ($commentInfo['type'] == commentModel::GOODS) {
				self::model('goods')->execute("update wf_goods set comment_total=comment_total-1 where goods_id={$commentInfo['type_id']}");
			}
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
	}
}